﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SZJT.Utility.DataSource
{
    public class QueryFilter
    {
        public enum FilterType
        {
            DATE,
            NUMERIC,
            TEXT
        }

        private string _strFilter = "";
        public string FilterString
        {
            get { return _strFilter; }
            set { _strFilter = value; }
        }

        private string GetValueMargin(FilterType fieldType)
        {
            string valueMargin = "";
            switch (fieldType)
            {
                case FilterType.DATE:
                //case GUID:
                case FilterType.TEXT:
                    valueMargin = "'";
                    break;
            }
            return valueMargin;
        }

        // AddFilter( "studentName", FilterType.TEXT, "张三" ) ==> {... and }studentName = '张三'
        // AddFilter( "studentAge", FilterType.NUMERIC, "20" ) ==> {... and }studentAge = 20
        public void AddFilter(string fieldName, FilterType fieldType, string fieldValue)
        {
            string valueMargin = GetValueMargin(fieldType);
            AddFilter(fieldName, fieldValue, "{0} = " + valueMargin + "{1}" + valueMargin);
        }

        // AddFilter( "studentName", "张三", "{0} = {1}") ==> 这么写是错的！ {... and }studentName = 张三
        // AddFilter( "studentName", "张三", "{0} = '{1}'") ==> 这么写才是对的！ {... and }studentName = '张三'
        // AddFilter( "studentName", "张三", "{0} Like '%{1}%'") ==> 这么写也是对的！ {... and }studentName Like '%张三%'
        public void AddFilter(string fieldName, string fieldValue, string filterExpress)
        {
            AddFilter(fieldName, fieldValue, filterExpress, true);
        }

        // AddFilter( "studentName", FilterType.TEXT, "张三", true ) ==> {... and }studentName = '张三'
        // AddFilter( "studentAge", FilterType.NUMERIC, "20", false ) ==> {(...) or )}studentAge = 20{)}
        public void AddFilter(string fieldName, FilterType fieldType, string fieldValue, bool isAndOperation)
        {
            string valueMargin = GetValueMargin(fieldType);
            AddFilter(fieldName, fieldValue, "{0} = " + valueMargin + "{1}" + valueMargin, isAndOperation);
        }

        // filterExpress 中 {0} 代表字段名(fieldName)，{1} 代表查询值(fieldValue)
        // AddFilter( "studentName", "", "{0} = {1}", true ) ==> 无操作，因为fieldValue为空
        // AddFilter( "studentAge", 20, "{0} >= {1}", true ) ==> {... and } studentAge >= 20
        // AddFilter( "studentID", "2012", "{0} Like '%{1}%', false ) ==> {(...) or (}studentID Like '%2012%'{)}
        // AddFilter( "studentBirthday", "2001-12-24", "Datediff('m', {0}, '{1}')>1", true ) ==> {... and Datediff('m',studentBirtdhay,
        public void AddFilter(string fieldName, string fieldValue, string filterExpress, bool isAndOperation)
        {
            if (!string.IsNullOrEmpty(fieldValue))
            {
                AddFilter(string.Format(filterExpress, fieldName, fieldValue), isAndOperation);
            }
        }

        public void AddFilter(string subFilterString)
        {
            AddFilter(subFilterString, true);
        }

        public void AddFilter(string subFilterString, bool isAndOperation)
        {
            if (!string.IsNullOrEmpty(subFilterString))
            {
                if (!string.IsNullOrEmpty(_strFilter))
                {
                    if (isAndOperation)
                    {
                        _strFilter += " and " + subFilterString;
                    }
                    else
                    {
                        _strFilter = "(" + _strFilter + ") or (" + subFilterString + ")";
                    }
                }
                else
                {
                    _strFilter = subFilterString;
                }
            }
        }
    }
}
